package bamin.com.bamindaijia.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.aiton.administrator.shane_library.shane.utils.GsonUtils;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import bamin.com.bamindaijia.R;
import bamin.com.bamindaijia.constant.Constant;
import bamin.com.bamindaijia.model.CityFee;
import cz.msebera.android.httpclient.Header;

public class ChargesNotesActivity extends ZjbBaseActivity implements View.OnClickListener {

    private TextView mTv_charge_wait_time;
    private TextView mTv_charge_wait_price;
    private TextView mTv_charge_title1;
    private TextView mTv_charge_start_time1;
    private TextView mTv_charge_base_price1;
    private TextView mTv_charge_base_km1;
    private TextView mTv_charge_unit_price1;
    private TextView mTv_charge_unit_km1;
    private TextView mTv_charge_title2;
    private TextView mTv_charge_start_time2;
    private TextView mTv_charge_base_price2;
    private TextView mTv_charge_base_km2;
    private TextView mTv_charge_unit_price2;
    private TextView mTv_charge_unit_km2;
    private TextView mTv_charge_title3;
    private TextView mTv_charge_start_time3;
    private TextView mTv_charge_base_price3;
    private TextView mTv_charge_base_km3;
    private TextView mTv_charge_unit_price3;
    private TextView mTv_charge_unit_km3;
    private String mCity;
    private AMapLocationClient mLocationClient;
    private AMapLocationClientOption mLocationOption;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation amapLocation) {
            if (amapLocation != null) {
                if (amapLocation.getErrorCode() == 0) {
                    //定位成功回调信息，设置相关消息
                    mCity = amapLocation.getCity();
                    loadCityFee(mCity);

                } else {
                    //显示错误信息ErrCode是错误码，errInfo是错误信息，详见错误码表。
                    Log.e("AmapError", "location Error, ErrCode:"
                            + amapLocation.getErrorCode() + ", errInfo:"
                            + amapLocation.getErrorInfo());
                }
            }
        }

    };
    private CityFee.DatasBean mDatas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_charges_notes);
        showLoadingDialog();
        init();
    }

    @Override
    protected void initIntent() {
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initData() {
        //初始化定位
        mLocationClient = new AMapLocationClient(getApplicationContext());
        //设置定位回调监听
        mLocationClient.setLocationListener(mLocationListener);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(true);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();

    }

    @Override
    protected void findID() {
        mTv_charge_wait_time = (TextView) findViewById(R.id.tv_charge_wait_time);
        mTv_charge_wait_price = (TextView) findViewById(R.id.tv_charge_wait_price);
        View mInclude_charge_time1 = findViewById(R.id.include_charge_time1);
        mTv_charge_title1 = (TextView) mInclude_charge_time1.findViewById(R.id.tv_charge_title);
        mTv_charge_start_time1 = (TextView) mInclude_charge_time1.findViewById(R.id.tv_charge_start_time);
        mTv_charge_base_price1 = (TextView) mInclude_charge_time1.findViewById(R.id.tv_charge_base_price);
        mTv_charge_base_km1 = (TextView) mInclude_charge_time1.findViewById(R.id.tv_charge_base_km);
        mTv_charge_unit_price1 = (TextView) mInclude_charge_time1.findViewById(R.id.tv_charge_unit_price);
        mTv_charge_unit_km1 = (TextView) mInclude_charge_time1.findViewById(R.id.tv_charge_unit_km);
        View mInclude_charge_time2 = findViewById(R.id.include_charge_time2);
        mTv_charge_title2 = (TextView) mInclude_charge_time2.findViewById(R.id.tv_charge_title);
        mTv_charge_start_time2 = (TextView) mInclude_charge_time2.findViewById(R.id.tv_charge_start_time);
        mTv_charge_base_price2 = (TextView) mInclude_charge_time2.findViewById(R.id.tv_charge_base_price);
        mTv_charge_base_km2 = (TextView) mInclude_charge_time2.findViewById(R.id.tv_charge_base_km);
        mTv_charge_unit_price2 = (TextView) mInclude_charge_time2.findViewById(R.id.tv_charge_unit_price);
        mTv_charge_unit_km2 = (TextView) mInclude_charge_time2.findViewById(R.id.tv_charge_unit_km);
        View mInclude_charge_time3 = findViewById(R.id.include_charge_time3);
        mTv_charge_title3 = (TextView) mInclude_charge_time3.findViewById(R.id.tv_charge_title);
        mTv_charge_start_time3 = (TextView) mInclude_charge_time3.findViewById(R.id.tv_charge_start_time);
        mTv_charge_base_price3 = (TextView) mInclude_charge_time3.findViewById(R.id.tv_charge_base_price);
        mTv_charge_base_km3 = (TextView) mInclude_charge_time3.findViewById(R.id.tv_charge_base_km);
        mTv_charge_unit_price3 = (TextView) mInclude_charge_time3.findViewById(R.id.tv_charge_unit_price);
        mTv_charge_unit_km3 = (TextView) mInclude_charge_time3.findViewById(R.id.tv_charge_unit_km);
    }

    @Override
    protected void initViews() {

    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageView_back).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView_back:
                finishTo();
                break;
        }
    }
    /**
     * 城市计费规则
     *
     * @param city
     */
    private void loadCityFee(String city) {
        String url = Constant.Url.GET_CITY_FEE;
        AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("city", city);
        asyncHttpClient.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                String s = new String(responseBody);
                Log.e("ChargesNotesActivity", "城市计费规则: --->>" + s);
                try {
                    CityFee cityFee = GsonUtils.parseJSON(s, CityFee.class);
                    if (cityFee.getStatus() == 0) {
                        mDatas = cityFee.getDatas();
                        setView();
                    }
                } catch (Exception e) {
                    Toast.makeText(ChargesNotesActivity.this, "数据解析错误", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                Log.e("ChargesNotesActivity", "网络请求失败");
                Toast.makeText(ChargesNotesActivity.this, "网络请求失败", Toast.LENGTH_SHORT).show();

            }
        });
    }
    private void setView() {
        mTv_charge_wait_time.setText(mDatas.getWaitUnitTime() + "");
        mTv_charge_wait_price.setText(mDatas.getWaitUnitPrice() + "");

        mTv_charge_title1.setText("时间段1");
        mTv_charge_start_time1.setText(mDatas.getIntervalOneStart() + "");
        mTv_charge_base_price1.setText(mDatas.getIntervalOneBasePrice() + "");
        mTv_charge_base_km1.setText(mDatas.getIntervalOneBaseMileage() + "");
        mTv_charge_unit_price1.setText(mDatas.getIntervalOneUnitPrice() + "");
        mTv_charge_unit_km1.setText(mDatas.getIntervalOneUnitMileage()+"");

        mTv_charge_title2.setText("时间段2");
        mTv_charge_start_time2.setText(mDatas.getIntervalTwoStart() + "");
        mTv_charge_base_price2.setText(mDatas.getIntervalTwoBasePrice() + "");
        mTv_charge_base_km2.setText(mDatas.getIntervalTwoBaseMileage() + "");
        mTv_charge_unit_price2.setText(mDatas.getIntervalTwoUnitPrice() + "");
        mTv_charge_unit_km2.setText(mDatas.getIntervalTwoUnitMileage()+"");

        mTv_charge_title3.setText("时间段3");
        mTv_charge_start_time3.setText(mDatas.getIntervalThreeStart() + "");
        mTv_charge_base_price3.setText(mDatas.getIntervalThreeBasePrice() + "");
        mTv_charge_base_km3.setText(mDatas.getIntervalThreeBaseMileage() + "");
        mTv_charge_unit_price3.setText(mDatas.getIntervalThreeUnitPrice() + "");
        mTv_charge_unit_km3.setText(mDatas.getIntervalThreeUnitMileage()+"");
        cancelLoadingDialog();

    }
}
